package com.qtt.app.service.impl;

import com.qtt.app.entity.AppStudyHistory;
import com.qtt.app.entity.CourseClass;
import com.qtt.app.entity.User;
import com.qtt.app.mapper.AppStudyHistoryMapper;
import com.qtt.app.mapper.CourseClassMapper;
import com.qtt.app.service.AppStudyHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppStudyHistoryServiceImpl implements AppStudyHistoryService {
    @Autowired
    AppStudyHistoryMapper appStudyHistoryMapper;
    @Autowired
    CourseClassMapper courseClassMapper;

    @Override
    public List<AppStudyHistory> getMyStudyHistoryAll(Long userId) {
        List<AppStudyHistory> entities = appStudyHistoryMapper.getMyStudyHistoryAll(userId);
        return entities;
    }

    @Override
    public Map<String, Object> addWatchOnlineCourseVideoRecord(AppStudyHistory requestData) {

        Map<String,Object> map = new HashMap<>();
        String generalId = requestData.getGeneralId();
        String orderId = requestData.getOrderId();
        Long userId = requestData.getUserId();

        int addFlag =0;
        //判断是否已经存在,存在则修改时间即可，不存在再添加
        AppStudyHistory isOnlyFlag = appStudyHistoryMapper.isOnly(requestData.getUserId(),generalId,orderId,requestData.getChapterName(),requestData.getVideoName());
        System.out.println("查找到的记录是："+isOnlyFlag);
        if(isOnlyFlag != null ){
            System.out.println("已存在该记录，id="+isOnlyFlag.getStudyHistoryId());

            requestData.setGmtModified(new Date());
            requestData.setStudyHistoryId(isOnlyFlag.getStudyHistoryId());
            addFlag = appStudyHistoryMapper.updateByPrimaryKeySelective(requestData);
            map.put("studyHistoryId",isOnlyFlag.getStudyHistoryId());
            int id = appStudyHistoryMapper.getLastId();
            //更新学习进度，获取观看历史表中有多少条该课程的记录
            CourseClass courseClassEntity = courseClassMapper.getCourseClassByCourseId(generalId);
            Integer totalClassNum = courseClassEntity.getClassHour();
            Integer alreadyStudyNum = appStudyHistoryMapper.getMyAlreadyStudyNum(userId,generalId,orderId);
            map.put("totalClassNum",totalClassNum);
            //添加记录成功才返回
            if(addFlag!=0){
                map.put("alreadyStudyNum",alreadyStudyNum) ;
                return map;
            }
            else{
                map.put("alreadyStudyNum",0) ;
                map.put("studyHistoryId",null);
                return map;
            }
        }
        else{
            System.out.println("未存在该记录");
            requestData.setGmtCreate(new Date());
            requestData.setGmtModified(new Date());
            addFlag = appStudyHistoryMapper.insertSelective(requestData);
            int id = appStudyHistoryMapper.getLastId();
            //更新学习进度，获取观看历史表中有多少条该课程的记录
            CourseClass courseClassEntity = courseClassMapper.getCourseClassByCourseId(generalId);
            Integer totalClassNum = courseClassEntity.getClassHour();
            Integer alreadyStudyNum = appStudyHistoryMapper.getMyAlreadyStudyNum(userId,generalId,orderId);
            map.put("totalClassNum",totalClassNum);
            //添加记录成功才返回
            if(addFlag!=0){
                map.put("alreadyStudyNum",alreadyStudyNum) ;
                map.put("studyHistoryId",id);
                return map;
            }
            else{
                map.put("alreadyStudyNum",0) ;
                map.put("studyHistoryId",null);
                return map;
            }
        }

    }

    @Override
    public AppStudyHistory getLastStudyCourse(Long userId) {

        AppStudyHistory appStudyHistoryEntity = appStudyHistoryMapper.getLastStudyCourse(userId);

        return appStudyHistoryEntity;
    }

}
